package com.nf.health.app.sport_recipe.fragment;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nf.health.app.R;
import com.nf.health.app.core.MyGsonBuilder;
import com.nf.health.app.customview.MyFlowLayout;
import com.nf.health.app.fragment.IBaseFragment;
import com.nf.health.app.http.Act;
import com.nf.health.app.http.RequestProvider;
import com.nf.health.app.sport_recipe.activity.SportRecipeActivity;
import com.nf.health.app.sport_recipe.activity.SportRecipeHisActivity;
import com.nf.health.app.sport_recipe.bean.SportUserInfoBean;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.DrawableUtils;

import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.baidu.location.b.g.r;

/**
 * Created by changpeng on 2017/4/18.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author changpeng
 * @Description 运动处方个人信息
 */

public class SportUserInfoFragment extends IBaseFragment implements SportRecipeActivity.IOpFragment{
    String mHpuid = "";
    @Bind(R.id.tv_base_info)
    TextView mTvBaseInfo;
    @Bind(R.id.my_flow_layout)
    MyFlowLayout mMyFlowLayout;
    @Bind(R.id.tv_base_info_bottom)
    TextView mTvBaseInfoBottom;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addViewFillInRoot(R.layout.fragment_sport_user_info);
        ButterKnife.bind(this, rootView);
        initView();
        initData();
    }

    private void initView() {

    }

    private void initData() {
        if(getActivity() instanceof SportRecipeActivity){
            provider.getYDSportsUsers(SportRecipeActivity.hpuid);
        }

        if(getActivity() instanceof SportRecipeHisActivity){
            provider.getYDSportsUsers(SportRecipeHisActivity.hpuid);
        }

    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if(Act.Flag.FLAG_YD_SPORTSUSERS.equals(actionName)){
            Gson gson = new MyGsonBuilder().createGson();
            SportUserInfoBean bean = gson.fromJson(object+"", new TypeToken<SportUserInfoBean>() {}.getType());
            setDataToView(bean);
        }
    }

    @Override
    public void handleActionError(String actionName, String response, VolleyError error) {
        super.handleActionError(actionName, response, error);
    }

    private void setDataToView(SportUserInfoBean bean){
        if(bean == null){
            return;
        }
        String time = DateUtil.date2String(bean.getTime(),"yyyy-MM-dd");
        mTvBaseInfo.setText(
                "姓名: "+bean.getName() + "   年龄: " + bean.getAge() + "   编号: " + bean.getNum() + "\n" +
                "时间: "+time + "   身高: "+bean.getHeight() + "   体重: "+bean.getWeight() + "\n" +
        "BMI: "+bean.getBmi()+"   最大心率: "+bean.getMaxheartrate() + " 次/分" + "   静息心率: "+bean.getReserveheartrate()+" 次/分");
        String[] target = bean.getManagetarget().split("-");
        initTipTextView(mMyFlowLayout,target);

        mTvBaseInfoBottom.setText("您在本次测试中:"+"\n" + "心脏功能能力 (F.C)  "+bean.getP3()+"\n" +
                "运动能力 (E.C)   "+bean.getP2() + "\n\n" +
                "您的有效储备心率范围 "+bean.getRestheartrate() + "次/分 "+bean.getP1());

    }

    private void initTipTextView(MyFlowLayout flowLayout,String[] strs) {
        int padding = DensityUtil.dip2px(getActivity(), 12);
        for (int i = 0; i < strs.length; i++) {
            final String keyword = strs[i];
            TextView view = new TextView(getActivity());
            view.setText(keyword);
            view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);// 18sp
            view.setGravity(Gravity.CENTER);

            view.setTextColor(getResources().getColor(R.color.theme_color));
            GradientDrawable bgNormal = DrawableUtils.getGradientDrawable(getResources().getColor(R.color.theme_color), DensityUtil.dip2px(getActivity(), 20));
            view.setBackgroundDrawable(bgNormal);
            view.setPadding(padding, DensityUtil.dip2px(getActivity(), 3), padding, DensityUtil.dip2px(getActivity(), 3));
            ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            marginLayoutParams.setMargins(DensityUtil.dip2px(getActivity(), 5), DensityUtil.dip2px(getActivity(), 10), DensityUtil.dip2px(getActivity(), 5), DensityUtil.dip2px(getActivity(), 10));// childView的间距
            flowLayout.addView(view, marginLayoutParams);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onAction(String hpuid) {
        mHpuid = hpuid;
        initData();
    }
}
